package com.gisuni.sddc.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONObject;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import com.gisuni.sddc.model.Response;
import com.gisuni.sddc.service.SpecialdataService;
import com.gisuni.sddc.util.StringUtil;

/**
 * 专题数据统计
 *
 */
@IocBean(singleton = false)
@At("/client/service/specialdataController")
public class SpecialdataController {

	
	private Log log = Logs.getLog(SpecialdataController.class);

	@Inject
	private SpecialdataService  specialdataService;

	/**
	 * 查询区域，行政区
	 * 
	 * @return
	 */
	@At("/queryAreaOrDistinct")
	@GET
	@POST
	@Ok("json")
	public Object queryAreaOrDistinct(@Param("type") String type,@Param("cityName") String cityName, HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
//			System.out.println(type);
			res = specialdataService.queryAreaOrDistinct(type,cityName);
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}
	
	/**
	 * 查询统计数据
	 * 
	 * @return
	 */
	@At("/querySpecialdataList")
	@GET
	@POST
	@Ok("json")
	public Object querySpecialdataList(@Param("cityName") String cityName,@Param("distinctName") String distinctName ,@Param("type") String type , HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
//				res = specialdataService.querySpecialdataList(cityName,distinctName,type);
				res = specialdataService.querySpecialdataList(cityName,distinctName );
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}
	 
	/**
	 * 导出Excel
	 * @param departmentType
	 * @param projectType
	 * @param startdate
	 * @param enddate
	 * @param response
	 */
	@At("/exportSpecialdataExcel")
	@POST
	@GET
	public void exportlandSpecialdataExcel(@Param("cityName") String cityName,@Param("distinctName") String distinctName,HttpServletResponse response) {
		try {
			if(!StringUtil.isNullOrEmpty(cityName)){
				cityName = new String(cityName.getBytes("iso8859-1"),"UTF-8");
			}
			if(!StringUtil.isNullOrEmpty(distinctName)){
				distinctName = new String(distinctName.getBytes("iso8859-1"),"UTF-8");
			}
			specialdataService.exportlandSpecialdataExcel(cityName,distinctName, response);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
	}
	
 
}
